package com.byedu.util;

import com.byedu.model.enums.QuestionTypeEnum;

import java.io.*;
import java.nio.file.Path;

/**
 * Created by jason on 2017/6/4.
 */
public class FileUtil {


    public static  void  createdir(String filePath){
        File file = new File(filePath);

        if(!file.exists()){
            file.mkdirs();
        }
    }
    /**
     * 单个文件上传
     * @param is
     * @param fileName
     * @param filePath
     */
    public static void uploadFile(InputStream is,String fileName,String filePath){
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        File file = new File(filePath);

        if(!file.exists()){
            file.mkdirs();
        }

        File f = new File(filePath+"/"+fileName);
        try {
            bis = new BufferedInputStream(is);
            fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos);
            byte[] bt = new byte[4096];
            int len;

            while((len = bis.read(bt))>0){
                bos.write(bt, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != bos){
                    bos.close();
                    bos = null;}
                if(null != fos){
                    fos.close();
                    fos= null;
                }
                if(null != is){
                    is.close();
                    is=null;
                }
                if (null != bis) {
                    bis.close();
                    bis = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    //递归删除文件夹
    public static void deleteFile(File file) {
        if (file.exists()) {//判断文件是否存在
            if (file.isFile()) {//判断是否是文件
                file.delete();//删除文件
            } else if (file.isDirectory()) {//否则如果它是一个目录
                File[] files = file.listFiles();//声明目录下所有的文件 files[];
                for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件
                    deleteFile(files[i]);//把每个文件用这个方法进行迭代
                }
                file.delete();//删除文件夹
            }
        } else {
            System.out.println("所删除的文件不存在");
        }
    }


    //重命名
    public static void renameFile(String path ,File file) {
        //命名规则

        if (file.exists()) {//判断文件是否存在
            if (file.isFile()) {//判断是否是文件

                String newName= path+"\\"+ rechar(file.getName());
                File newfile = new File(newName);
                if(newfile.exists()){
                System.out.print(newfile.getName()+"存在了");
                }else {
                    file.renameTo(newfile);
                    System.out.println(newfile.getName()+"重新命名成功了");
                }

            } else if (file.isDirectory()) {//否则如果它是一个目录
                File[] files = file.listFiles();//声明目录下所有的文件 files[];
                for (int i = 0;i < files.length;i ++) {//遍历目录下所有的文件
                    renameFile(path+"\\"+files[i].getName(),files[i]);//把每个文件用这个方法进行迭代
                }
            }
        } else {
            System.out.println("需要重新命名的文件不存在");
        }
    }


    //命名规则
    public static String rechar(String oldName){
        String sp[] = oldName.split("\\.");

        String endStr ="单选题";
        //1.去掉空格
        String content = sp[0].trim() ;
        if(sp[0].trim().indexOf(QuestionTypeEnum.single.getTitle()) != -1){
             endStr =QuestionTypeEnum.single.getTitle();
        }
        if(sp[0].trim().indexOf(QuestionTypeEnum.multiple.getTitle()) != -1){
            endStr =QuestionTypeEnum.multiple.getTitle();
        }
        if(sp[0].trim().indexOf(QuestionTypeEnum.judgments.getTitle()) != -1){
            endStr =QuestionTypeEnum.judgments.getTitle();
        }
        content = content.replace("单选题","");
        content = content.replace("多选题","");
        content = content.replace("判断题","");
        content = content.replace("单选","");
        content = content.replace("多选","");
        content = content.replace("判断","");


        String[] befoStr = content.split("章",2);
        String startStr = "1";
        if(befoStr.length == 2){
            content = befoStr[1];
            startStr = befoStr[0];
        }else{
            content = befoStr[0];
            startStr = befoStr[0];
        }
        content = content.replace("_","");
        for (int i=30;i>-1;i--) {
            content.replaceAll(String.valueOf(i), "");
        }

        for (int i=30;i>-1;i--){

            if(startStr.trim().indexOf(String.valueOf(i)) != -1){
                startStr = String.valueOf(i);
                break;
            }
            switch (i){
                case 1 :
                    if("第一".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 2 :
                    if("第二".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 3 :
                    if("第三".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 4 :
                    if("第四".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 5 :
                    if("第五".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 6 :
                    if("第六".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 7 :
                    if("第七".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 8 :
                    if("第八".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 9 :
                    if("第九".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 10 :
                    if("第十".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 11 :
                    if("第十一".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 12 :
                    if("第十二".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 13 :
                    if("第十三".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 14 :
                    if("第十四".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 15 :
                    if("第十五".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 16 :
                    if("第十六".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 17 :
                    if("第十七".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 18 :
                    if("第十八".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 19 :
                    if("第十九".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
                case 20 :
                    if("第二十".indexOf(startStr.trim()) != -1){
                        startStr = String.valueOf(i);
                    }
                    break;
            }

            startStr.replaceAll("第","");
        }

        String newName= startStr.trim()+"_"+content.trim()+"_"+endStr.trim()+"."+sp[1];
        if(befoStr.length == 2) {

        }
                return newName;
    }

}
